#include "MethodSubtraction.h"

using namespace FitMethods;

FitResult MethodSubtraction::runAnalysisCase(AnalysisConfig config, FitInput input) {
    /** WARNING: not sure what to do for many signal or background templates,
     * maybe someday this should be implemented */
    if(input.getSignalTemplates().size()!=0 || input.getBackgroundTemplates().size()!=1) {
        throw FitMethodException("MethodSubtraction needs exactly zero signal template and exactly one background template, but was called with "+boost::lexical_cast<string>(input.getSignalTemplates().size())+" signal templates and "+boost::lexical_cast<string>(input.getBackgroundTemplates().size())+"background templates");
    }
    TH1D * signalTemplate = input.getSignalTemplates()[0];
    TH1D * backgroundTemplate = input.getBackgroundTemplates()[0];
    RooRealVar * isol = new RooRealVar("isol","E_{T}^{cone40}/E_{T}",-0.2,1.5);
    gc->add(isol);
    RooRealVar & nSignal = *new RooRealVar("nSignal", "amount of signal events", 0, 0, 1000000000);
    gc->add(&nSignal);
    RooRealVar & nBackground = *new RooRealVar("nBackground", "amount of background events", 0, 0, 1000000000);
    gc->add(&nBackground);
    Utility::subtrBgdFromData(input.getData(),backgroundTemplate,nBackground,nSignal,config.getIsolThr());
    RooRealVar & nBgdIsolTh = *new RooRealVar("nBgdIsolTh", "amount of background events below threshold", 0, 0, 1000000000);
    gc->add(&nBgdIsolTh);
    RooRealVar & nSigIsolTh = *new RooRealVar("nSigIsolTh", "amount of signal events below threshold", 0, 0, 1000000000);
    gc->add(&nSigIsolTh);
    Utility::calcCountBelowIsolTh(input.getData(),backgroundTemplate,nBgdIsolTh,nSigIsolTh,config.getIsolThr());
    /// WARNING: probably using Isolation here was a mistake
    RooDataHist * dataHist = new RooDataHist("dataPlot", "Isolation", *isol, input.getData());
    gc->add(dataHist);
    RooDataHist * backgroundTemplateHist = new RooDataHist("backgroundTemplatePlot", "Isolation", *isol, backgroundTemplate);
    gc->add(backgroundTemplateHist);
    RooHistPdf * backgroundPDF = new RooHistPdf("backgroundPDF", "Isolation", *isol, *backgroundTemplateHist);
    gc->add(backgroundPDF);
    vector <RooAbsPdf *> backgroundPdfs, signalPdfs;
    backgroundPdfs.push_back(backgroundPDF);
    vector <RooRealVar *> signalVector;
    signalVector.push_back(&nSignal);
    vector <RooRealVar *> backgroundVector;
    backgroundVector.push_back(&nBackground);
    /// WARNING: this should be solved some other way than creating a new variable just to return it
    RooDataHist * rooDataHist = new RooDataHist("rooDataHist", "Isolation", *isol, input.getData());
    gc->add(rooDataHist);
    FitResult res("Subtraction Method",signalVector,backgroundVector);
    vector <RooRealVar *> nBgdIsolThVector, nSigIsolThVector;
    nBgdIsolThVector.push_back(&nBgdIsolTh);
    nSigIsolThVector.push_back(&nSigIsolTh);
    res.setBgdBelowIsolTh(nBgdIsolThVector);
    res.setSigBelowIsolTh(nSigIsolThVector);
    return res;
}
